package com.selen.controller;

import cn.hutool.core.bean.BeanUtil;
import com.selen.dto.PageDTO;
import com.selen.dto.RoomInsertDTO;
import com.selen.dto.RoomPageDTO;
import com.selen.dto.RoomUpdateDTO;
import com.selen.entities.Room;
import com.selen.excel.RoomExcel;
import com.selen.service.RoomService;
import com.selen.utils.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Selen
 */
@Tag(name = "房间模块")
@RestController
@RequestMapping("/api/v1/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @Operation(summary = "插入一条记录")
    @PostMapping("/save")
    public Object save(@RequestBody @Validated RoomInsertDTO dto){
        return roomService.insert(dto);
    }

    @Operation(summary = "查询一条记录")
    @GetMapping("/getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return roomService.selectById(id);
    }

    @Operation(summary = "修改一条记录")
    @PostMapping("/update")
    public Object update(@RequestBody @Validated RoomUpdateDTO dto){
        return roomService.updateById(dto);
    }

    @Operation(summary = "删除一条记录")
    @PostMapping("/remove/{id}")
    public Object update(@PathVariable Long id){
        return roomService.logicDeleteById(id);
    }

    @Operation(summary = "批量删除记录")
    @PostMapping("/removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){
        return roomService.logicDeleteByIds(ids);
    }

    @Operation(summary = "查询全部记录")
    @GetMapping("/list")
    public Object list(){
        return roomService.selectAll();
    }

    @Operation(summary = "分页查询记录")
    @PostMapping("/page")
    public Object page(@RequestBody @Validated PageDTO dto){
        return roomService.page(dto);
    }

    @SneakyThrows
    @Operation(summary = "打印数据")
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<RoomExcel> roomExcels = new ArrayList<>();
        List<Room> rooms = roomService.selectAll();
        for (Room room : rooms) {
            RoomExcel roomExcel = BeanUtil.copyProperties(room, RoomExcel.class);
            roomExcels.add(roomExcel);
        }
        EasyExcelUtil.download(resp, "房间统计表.xlsx", "sheet01",
                RoomExcel.class, roomExcels);
    }

    @Operation(summary = "名称模糊查询")
    @PostMapping("/dimSelect")
    public Object dimSelect(@RequestBody @Validated RoomPageDTO dto){
        return roomService.dimSelect(dto);
    }

}
